package com.zkthink.ceres.promotion.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.request.PageParams;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.ceres.promotion.dao.SalesCustomerMapper;
import com.zkthink.ceres.promotion.dto.SalesCustomerPageDTO;
import com.zkthink.ceres.promotion.dto.SalesCustomerUpdateDTO;
import com.zkthink.ceres.promotion.entity.SalesCustomer;
import com.zkthink.ceres.promotion.service.SalesCustomerService;
import com.zkthink.ceres.promotion.vo.admin.SalesCustomerAdminPageVO;
import com.zkthink.context.BaseContextHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 业务实现类 分销员
 * </p>
 *
 * @author kellen
 * @date 2020-08-22
 */
@Slf4j
@Service
public class SalesCustomerServiceImpl extends
    SuperServiceImpl<SalesCustomerMapper, SalesCustomer> implements SalesCustomerService {

  @Override
  public IPage<SalesCustomerAdminPageVO> page(PageParams<SalesCustomerPageDTO> params) {
    SalesCustomerPageDTO pageDTO = params.getModel();
    pageDTO.setTenantCode(BaseContextHandler.getTenant());
    return this.baseMapper.page(params.getPage(), pageDTO);
  }

  @Override
  public R updateCustomer(SalesCustomerUpdateDTO updateDTO) {
    //更新id查询信息
    SalesCustomer customer = this.getById(updateDTO.getId());
    if (customer == null) {
      return R.validFail("客户关系不存在");
    }
    //操作类型 1绑定  2解绑 3移除
    switch (updateDTO.getHandleType()) {
      case 1:
        //更新绑定id
        if (updateDTO.getSid() == null) {
          return R.validFail("分销员id不允许为空");
        }
        customer.setSid(updateDTO.getSid());
        this.updateById(customer);
        return R.success();
      case 2:
        customer.setSid(0L);
        this.updateById(customer);
        return R.success();
      default:
        this.removeById(customer);
    }
    return R.fail("非法的操作类型");
  }
}
